package com.firewolf.solr.s06_mlk;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Before;
import org.junit.Test;

/**
 * MLT 处理器
 * @author liuxing
 *
 */
public class MLTHandler {
	
	private HttpSolrClient client = null;
	
	@Before
	public void init() {
		// 获取solr连接，mycore1是集合名
		client = new HttpSolrClient("http://localhost:8081/solr/mycore3");
		System.out.println(client);
	}

	@Test
	public void mlt() throws Exception {
		SolrQuery q = new SolrQuery();
		q.setRequestHandler("/mlt");//设置请求处理器
		q.set("q", "id:1004");
		q.setParam("mlt", "true") // 开始相似搜索
				.setParam("mlt.fl", "name") // 需要判断相似度的域
				.setParam("mlt.mindf", "1") //
				.setParam("mlt.mintf", "1")
				.setRows(3);// 设置返回相似文章个数，和之前设置不同
		
		QueryResponse query = client.query(q);
		SolrDocumentList results = query.getResults(); //直接这样获取就可以
		for(SolrDocument r : results){
			System.out.println(r);
		}
	}
}
